package com.waves.task.strategy.receive;

import com.waves.core.constant.MQConstant;
import com.waves.core.utils.LoginUtils;
import com.waves.core.utils.StringUtils;
import com.waves.core.web.domain.R;
import com.waves.core.web.domain.entities.User;
import com.waves.pay.domain.dto.AlipayDto;
import com.waves.pay.service.AlipayService;
import com.waves.task.dao.TaskReceiveDao;
import com.waves.task.domain.entity.Task;
import com.waves.task.domain.entity.TaskReceive;
import com.waves.task.domain.enums.ReceiveTypeEnum;
import com.waves.task.domain.enums.TaskReceiveStatusEnum;
import com.waves.task.domain.enums.TaskTypeEnum;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

public abstract class AbstractReceiveStrategy {

    @Resource
    private TaskReceiveDao taskReceiveDao;

    @Resource
    private AlipayService alipayService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    private void init() {
        ReceiveFactory.register(getReceiveTypeEnum().getType(), this);
    }

    abstract ReceiveTypeEnum getReceiveTypeEnum();

    public abstract R<Object> receiveTask(Task task);

    public abstract void cancelTask(Task task);

    public abstract void createTask(Task task);

    /**
     * 开始插入任务领取数据
     *
     * @param task
     * @return
     */
    public R<Object> saveTaskReceive(Task task) {
        String payHtml = null;
        User user = LoginUtils.getUser();
        TaskReceive taskReceive = new TaskReceive()
                .setTaskId(task.getId())
                .setStatus(TaskReceiveStatusEnum.RECEIVE_STATUS_ING.getType())
                .setReceiverId(user.getId())
                .setReceiverName(user.getName())
                .setReceiveTime(new Date().getTime());
        taskReceiveDao.save(taskReceive);
        // 发送一个消息给延迟队列，如果超时则自动取消任务
        rabbitTemplate.convertAndSend(MQConstant.ORDER_DELAY_EXCHANGE, "stock.locked", taskReceive);
        if (Objects.equals(TaskTypeEnum.TASK_TYPE_HELP.getType(), task.getType())) {
            payHtml = getPayHtml(task, taskReceive);
        }
        return R.ok(payHtml);
    }

    /**
     * 如果是求助贴，主要返回支付页面
     *
     * @param task
     * @param taskReceive
     * @return
     */
    public String getPayHtml(Task task, TaskReceive taskReceive) {
        if (Objects.equals(TaskTypeEnum.TASK_TYPE_HELP.getType(), task.getType())) {
            // 当类型是求助贴 需要主动给发帖人支付
            AlipayDto alipayDto = new AlipayDto();
            alipayDto.setSubject(task.getTitle()).setId(task.getId())
                    .setBounty(task.getHelpBounty()).setBountyNum(task.getHelpBountyNum());
            alipayDto.setReceiveId(taskReceive.getId());
            String payHtml = alipayService.pay(alipayDto);
            taskReceive.setStatus(TaskReceiveStatusEnum.RECEIVE_STATUS_CANCEL.getType());
            return payHtml;
        }
        return null;
    }
}
